package com.ymf.consumer.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.ymf.consumer.pojo.Description;
import com.ymf.consumer.pojo.MyMessage;
import com.ymf.consumer.pojo.Skills;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/consumer")
@Slf4j
@DefaultProperties(defaultFallback = "defaultFallback")
public class ConsumerController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private DiscoveryClient discoveryClient;

    @GetMapping("all")
    //@HystrixCommand(fallbackMethod = "findallRallback")
    @HystrixCommand
    public String findall(){
        //获取euruka中注册的page-service的实例
//        List<ServiceInstance> serviceInstances = discoveryClient.getInstances("page-service");
//        ServiceInstance serviceInstance = serviceInstances.get(0);

//        return restTemplate.getForObject("http://"+serviceInstance.getHost()+":"+serviceInstance.getPort()+"/My/all",String.class);
        return restTemplate.getForObject("http://page-service/My/all",String.class);
    }
    //描述查询
    @GetMapping("desc/{id}")
    @HystrixCommand
    public String desc(@PathVariable int id){
        return restTemplate.getForObject("http://page-service/My/desc/"+id,String.class);
    }
    //关于我更新
    @PostMapping("updataAboutMe")
//    @HystrixCommand
    public String updataAboutMe(@RequestBody MyMessage myMessage){
//        System.out.println("123456789456138====================="+myMessage.getAbout());
        return restTemplate.postForObject("http://page-service/My/updataAboutMe",myMessage,String.class);
//        return restTemplate.getForObject("http://page-service/My/updataAboutMe/"+myMessage.getAbout(),String.class);
    }
    //更新个人信息
    @PostMapping("updatamy_message")
//    @HystrixCommand
    public String updataMy_message(@RequestBody MyMessage myMessage){
        return restTemplate.postForObject("http://page-service/My/updatamy_message",myMessage,String.class);
    }
    //查询技能表
    @GetMapping("findskills")
    @HystrixCommand
    public String findskills(){
        return restTemplate.getForObject("http://page-service/My/findskills",String.class);
    }
    //根据id删除技能表
    @GetMapping("deleteskillsbyid/{id}")
    public String deleteSkillsById(@PathVariable int id){
        return restTemplate.getForObject("http://page-service/My/deleteskillsbyid/"+id,String.class);
    }
    //插入数据到技能表
    @PostMapping("insertskills")
    public String insertSkills(@RequestBody Skills skills){
        return restTemplate.postForObject("http://page-service/My/insertskills",skills,String.class);
    }
    //修改技能表数据
    @PostMapping("updateskills")
    public String updateSkillsById(@RequestBody Skills skills){
        return restTemplate.postForObject("http://page-service/My/updateskills/",skills,String.class);
    }
    //修改个性签名
    @PostMapping("updatasignature")
    public String updataSignature(@RequestBody MyMessage myMessage){
        return restTemplate.postForObject("http://page-service/My/updatasignature/",myMessage,String.class);
    }
    //删除标签
    @PostMapping("deleteescription")
    public String deleteEscription(@RequestBody Description description){
        return restTemplate.postForObject("http://page-service/My/deleteescription",description,String.class);
    }
    //添加标签
    @PostMapping("insertdescription")
    public String insertDescription(@RequestBody Description description){
        return restTemplate.postForObject("http://page-service/My/insertdescription",description,String.class);
    }
    //修改公告
    @PostMapping("updatenotice")
    public String updateNotice(@RequestBody MyMessage myMessage){
        return restTemplate.postForObject("http://page-service/My/updatenotice",myMessage,String.class);
    }
    //修改头像
    @Value("${file.path}")
    private String path;
    @PostMapping("uodateavatar")
    public String uodateAvatar(@RequestBody MyMessage myMessage){
        myMessage.setAvatar("http://127.0.0.1:10001/file/"+myMessage.getAvatar());
        return restTemplate.postForObject("http://page-service/My/uodateavatar",myMessage,String.class);
    }


    @GetMapping("redis")
    public String findredis(){
        return restTemplate.getForObject("http://page-service/My/redis",String.class);
    }

    public String findallRallback(){
        log.error("查询用户信息失败。");
        return "对不起网络太拥挤了";
    }

    public String defaultFallback(){
        return "默认提示：对不起网络太拥挤了";
    }
}
